package org.veridiandynamics.client.view;

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.veridiandynamics.server.dao.HibernateDriver;
import org.veridiandynamics.server.dao.Queryprocessor;
import org.veridiandynamics.server.domain.DoctorsOrders;
import org.veridiandynamics.server.domain.Patient;
import org.veridiandynamics.server.domain.TreatmentRecord;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

/**
 * This class creates the gui for creating treatment records accessed 
 * by the nurse panel.
 * 
 * @author Lee
 */
public class CreateTreatmentRecordPanel extends AbstractPanelLayout
{
	private static final long serialVersionUID = 1L;
	private JButton back;
	private Container container;
	private CardLayout c1;
	private JTextField patientField;
	private JTextField symptomsField;
	private JTextField prescriptionField;
	private JTextField followUpField;
	private JTextField otherField;
	private JTextField labWorkField;
	private JButton submit;
	private Map<String, Object> treatmentRecordMap;
	private JTextField bloodPressureField;
	private JTextField pulseField;
	private JTextField heightField;
	private JTextField weightField;
	private String[] minVals;
	private String[] hourVals;
	private String[] monthVals;
	private String[] dayVals;
	private String[] yearVals;
	private JComboBox monthBox;
	private JComboBox dayBox;
	private JComboBox yearBox;
	private JComboBox hourBox;
	private JComboBox minBox;
	private JTextField temperatureField;
	private JTextField textField;

	/**
	 * The constructor for the create treatment records panel.  Initializes 
	 * the contents of the panel.
	 * 
	 * @param c1
	 * @param container
	 */
	public CreateTreatmentRecordPanel(final CardLayout c1, final Container container)
	{
		minVals = new String[4];
		minVals[0] = "00";
		minVals[1] = "15";
		minVals[2] = "30";
		minVals[3] = "45";

		hourVals = new String[10];
		for (int i = 0; i < hourVals.length; i++)
		{
			if (i < 5)
				hourVals[i] = (8 + i) + "";
			else
				hourVals[i] = (i - 4) + "";
		}

		monthVals = new String[12];
		monthVals[0] = "Jan";
		monthVals[1] = "Feb";
		monthVals[2] = "Mar";
		monthVals[3] = "Apr";
		monthVals[4] = "May";
		monthVals[5] = "Jun";
		monthVals[6] = "Jul";
		monthVals[7] = "Aug";
		monthVals[8] = "Sep";
		monthVals[9] = "Oct";
		monthVals[10] = "Nov";
		monthVals[11] = "Dec";

		dayVals = new String[32];
		for (int i = 1; i < dayVals.length; i++)
		{
			dayVals[i] = i + "";
		}

		Calendar calendar = Calendar.getInstance();
		yearVals = new String[2];
		yearVals[0] = "2011";
		yearVals[1] = "2012";

		numCenterPanels = 1;
		this.c1 = c1;
		this.container = container;
		initialize();

		MainWindow.welcomeMessage.setText("Create Treatment Record");
		panelLeft.setLayout(new BoxLayout(panelLeft, BoxLayout.Y_AXIS));
		JPanel subLeft = new JPanel();
		subLeft.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
				new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(21dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(15dlu;default):grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));

		JLabel lblDateOfAppointment = new JLabel("Date of Appointment:");
		subLeft.add(lblDateOfAppointment, "4, 2");

		JLabel lblTime = new JLabel("Time:");
		subLeft.add(lblTime, "8, 2");

		JPanel panel_1 = new JPanel();
		subLeft.add(panel_1, "4, 4, left, fill");

		monthBox = new JComboBox(monthVals);
		panel_1.add(monthBox);

		dayBox = new JComboBox(dayVals);
		panel_1.add(dayBox);

		yearBox = new JComboBox(yearVals);
		panel_1.add(yearBox);

		JPanel panel_2 = new JPanel();
		subLeft.add(panel_2, "8, 4, left, fill");

		hourBox = new JComboBox(hourVals);
		panel_2.add(hourBox);

		minBox = new JComboBox(minVals);
		panel_2.add(minBox);

		JLabel lblWhichPatient = new JLabel("Please enter the patient's username");
		subLeft.add(lblWhichPatient, "4, 6");

		JLabel lblDiagnosis = new JLabel("Diagnosis:");
		subLeft.add(lblDiagnosis, "8, 6");

		patientField = new JTextField();
		subLeft.add(patientField, "4, 8, fill, default");
		patientField.setColumns(10);

		textField = new JTextField();
		subLeft.add(textField, "8, 8, fill, default");
		textField.setColumns(10);

		JLabel lblWhatWereHis = new JLabel("What were his symptoms?");
		subLeft.add(lblWhatWereHis, "4, 10");

		symptomsField = new JTextField();
		subLeft.add(symptomsField, "4, 12, 9, 1, fill, default");
		symptomsField.setColumns(10);

		JLabel lblWhatWereHis_1 = new JLabel("What were his Vital Signs?");
		subLeft.add(lblWhatWereHis_1, "4, 14");

		JPanel panel_3 = new JPanel();
		subLeft.add(panel_3, "4, 16, 11, 1, left, fill");

		JLabel lblTemperature = new JLabel("Temperature: ");
		panel_3.add(lblTemperature);

		temperatureField = new JTextField();
		panel_3.add(temperatureField);
		temperatureField.setColumns(10);

		JLabel lblBloodPressure = new JLabel("Blood pressure: ");
		panel_3.add(lblBloodPressure);

		bloodPressureField = new JTextField();
		panel_3.add(bloodPressureField);
		bloodPressureField.setColumns(7);

		JPanel panel = new JPanel();
		subLeft.add(panel, "4, 18, 11, 1, left, fill");

		JLabel lblPulse = new JLabel("Pulse: ");
		panel.add(lblPulse);

		pulseField = new JTextField();
		panel.add(pulseField);
		pulseField.setColumns(7);

		JLabel lblWeight = new JLabel("Weight: ");
		panel.add(lblWeight);

		weightField = new JTextField();
		panel.add(weightField);
		weightField.setColumns(7);

		JLabel lblHeight = new JLabel("Height");
		panel.add(lblHeight);

		heightField = new JTextField();
		panel.add(heightField);
		heightField.setColumns(7);

		JLabel lblWhatWereThe = new JLabel("What were the Doctors Orders?");
		subLeft.add(lblWhatWereThe, "4, 20");

		JLabel lblPrescriptions = new JLabel("Prescriptions");
		subLeft.add(lblPrescriptions, "4, 22");

		JLabel lblFollowup = new JLabel("FollowUp");
		subLeft.add(lblFollowup, "8, 22");

		JLabel lblOtherInstructions = new JLabel("Other Instructions");
		subLeft.add(lblOtherInstructions, "14, 22");

		prescriptionField = new JTextField();
		subLeft.add(prescriptionField, "4, 24, fill, default");
		prescriptionField.setColumns(10);

		followUpField = new JTextField();
		subLeft.add(followUpField, "8, 24, fill, default");
		followUpField.setColumns(10);

		otherField = new JTextField();
		subLeft.add(otherField , "14, 24, fill, default");
		otherField .setColumns(10);

		JLabel lblLabworkPerformed = new JLabel("LabWork Performed");
		subLeft.add(lblLabworkPerformed, "4, 26");

		labWorkField = new JTextField();
		subLeft.add(labWorkField, "4, 28, 10, 1, fill, default");
		labWorkField.setColumns(10);

		panelLeft.add(subLeft);

		JPanel subLeft2 = new JPanel();
		back = new JButton("Back");
		submit = new JButton("Submit");
		back.addActionListener(new ActionListener() 
		{
			/**
			 * This method moves the user back to the nurse panel.
			 * 
			 * @param event The ActionEvent object handling the listening.
			 */
			public void actionPerformed(ActionEvent event) 
			{
				c1.first(container);
				MainWindow.welcomeMessage.setText("Welcome");
			}
		});
		subLeft2.add(submit);
		ButtonListener listener = new ButtonListener();
		submit.addActionListener(listener);
		subLeft2.add(back);
		panelLeft.add(subLeft2);

	}

	/**
	 * Private button listener class that handles the listening for all the widgets 
	 * on the create treatment panel.
	 * 
	 * @author Lee
	 */
	private class ButtonListener implements ActionListener
	{
		/**
		 * The requisite implemented method for the actionlistener interface.
		 */
		public void actionPerformed(ActionEvent event)
		{
			if (event.getSource() == submit)
			{
				if (!(patientField.getText().equals("") || 
						symptomsField.getText().equals("") || prescriptionField.getText().equals("") || 
						bloodPressureField.getText().equals("") || followUpField.getText().equals("") || 
						otherField.getText().equals("") || labWorkField.getText().equals("") || 
						heightField.getText().equals("") || weightField.getText().equals("") || 
						pulseField.getText().equals("") || temperatureField.getText().equals("")))
				{
					String theMonth = monthVals[monthBox.getSelectedIndex()];
					int month = PatientPanel.convert(theMonth);
					String day = dayVals[dayBox.getSelectedIndex()];
					String year = yearVals[yearBox.getSelectedIndex()];
					String hours = hourVals[hourBox.getSelectedIndex()];
					String mins = minVals[minBox.getSelectedIndex()];
					String patient = patientField.getText();
					String symptoms = symptomsField.getText();
					String prescription = prescriptionField.getText();
					try {
						Integer.parseInt(bloodPressureField.getText());
						Integer.parseInt(pulseField.getText());
						Integer.parseInt(weightField.getText());
					}
					catch(NumberFormatException e)
					{
						JOptionPane.showMessageDialog(null, "Please enter integers" + 
						" in the fields for bloodpressure, pulse and weight");
						//	Logger logger = new Logger();
					}
					String bloodPressure = bloodPressureField.getText();
					String pulse = pulseField.getText();
					String weight = weightField.getText();
					String height = heightField.getText();
					String followUp = followUpField.getText();
					String other = otherField.getText();
					String labWork = labWorkField.getText();
					String temperature = temperatureField.getText();
					String diagnosis = textField.getText();

					DoctorsOrders orders = new DoctorsOrders(other, prescription, 
							labWork, followUp);
					Map<String, String> vitalSigns = new HashMap<String, String>();
					vitalSigns.put("blood pressure", bloodPressure);
					vitalSigns.put("height", height);
					vitalSigns.put("weight", weight);
					vitalSigns.put("pulse", pulse);
					vitalSigns.put("temperature", temperature);
					String username = patientField.getText();
					Patient patient2 = new Patient();
					long id = Queryprocessor.UsernametoID(username);
					HibernateDriver driver = new HibernateDriver();
					patient2 = (Patient) driver.RetrieveObject(id, patient2);
					Date theDate = new Date(Integer.parseInt(year) - 1900, 
							month, Integer.parseInt(day), 
							Integer.parseInt(hours), Integer.parseInt(mins));
					TreatmentRecord record = new TreatmentRecord(null, orders, 
							symptoms, vitalSigns, diagnosis, patient2, theDate);
					//driver.SaveObject(record);
					patient2.getMyTreatmentRecordList().add(record);
					driver.UpdateObject(patient2);
					
					JOptionPane.showMessageDialog(null, "Treatment Record created");
				}

				else JOptionPane.showMessageDialog(null, "Please fill out all forms");
			}
		}
	}
}